package com.example.dllo.exe.fenlei.danpin;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.DanPinRightBean;
import com.example.dllo.exe.fengzhuang.NetTool;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class DanpinRVRightAdapter extends RecyclerView.Adapter<DanpinRVRightAdapter.MyViewHolder> {
    private Context context;
    private DanPinRightBean danPinRightBean;
    private int i;
    private Itemclick itemonClick;

    public void setItemonClick(Itemclick itemonClick) {
        this.itemonClick = itemonClick;
    }

    public  interface  Itemclick{
        void Itemclick(int pos, String name);
    }
    public void setI(int i) {
        this.i = i;
    }

    public DanpinRVRightAdapter(Context context) {
        this.context = context;
    }

    public void setDanPinRightBean(DanPinRightBean danPinRightBean) {
        this.danPinRightBean = danPinRightBean;
        notifyDataSetChanged();
    }

    @Override
    public DanpinRVRightAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.danpinrvrightinlayout, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DanpinRVRightAdapter.MyViewHolder holder, final int position) {
        try {
            NetTool netTool = new NetTool();
            netTool.getImage(danPinRightBean.getData().getCategories().get(i).getSubcategories().get(position).getIcon_url(), holder.danpinrvrightimage);
            holder.danpinrvrighttext.setText(danPinRightBean.getData().getCategories().get(i).getSubcategories().get(position).getName());

           holder.danpinrvrightimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String name = danPinRightBean.getData().getCategories().get(i).getSubcategories().get(position).getName();
                    itemonClick.Itemclick(position, name );
                }
            });
            holder.danpinrvrighttext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String name = danPinRightBean.getData().getCategories().get(i).getSubcategories().get(position).getName();
                    itemonClick.Itemclick(position, name);

                }
            });
        } catch (Exception e) {

        }

    }

    @Override
    public int getItemCount() {
        return danPinRightBean != null ? danPinRightBean.getData().getCategories().get(i).getSubcategories().size() : 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView danpinrvrighttext;
        ImageView danpinrvrightimage;

        public MyViewHolder(View itemView) {
            super(itemView);

            danpinrvrighttext = (TextView) itemView.findViewById(R.id.febleidanpinright_text);
            danpinrvrightimage = (ImageView) itemView.findViewById(R.id.febleidanpinright_image);
        }
    }
}
